package com.hmall.item.web;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hmall.common.dto.PageDTO;
import com.hmall.item.pojo.Item;
import com.hmall.item.service.IItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("item")
public class ItemController {

    @Autowired
    private IItemService itemService;

    /**
     * 分页查询
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/list")
    public PageDTO<Item> page(@RequestParam ("page")Integer page, @RequestParam("size") Integer size){

        IPage<Item> ipage = new Page<>(page,size);
        itemService.page(ipage);

        PageDTO<Item> pageDTO =new PageDTO<>();
        pageDTO.setList(ipage.getRecords());
        pageDTO.setTotal(ipage.getTotal());
         return pageDTO;

        /*itemService.pageItem(page,size);

        return*/

    }


    /**
     * 新增商品
     * @param item
     */

    @PostMapping
    public void  addItem(@RequestBody  Item item){
        itemService.addItem(item);


    }

    /**
     *修改商品
     * @param id
     * @param status
     */
    @PutMapping("/status/{id}/{status}")
    public  void  upOrDownItem(@PathVariable Long id, @PathVariable Integer status){
        itemService.upOrDownItem(id,status);


    }

    /**
     * 删除商品
     * @param id
     */
    @DeleteMapping("/{id}")
    public void  deleteItem(@PathVariable  Long id){
      //itemService.removeById(id);
        itemService.deleteItem(id);

    }

    /**
     * 查询id
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Item queryItem(@PathVariable Long id){
        Item byId = itemService.getById(id);
        return byId;

    }




    
}
